//
//  ProductDetailVC.swift
//  RPay
//
//  Created by CharlieLiao on 2017/7/23.
//  Copyright © 2017年 Charlie. All rights reserved.
//

import MBProgressHUD

class ProductDetailVC: BaseVC {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view.
        
        title = LocalStr("商品详情")
        
        let img = UIImage(named: "icon_shopping_normal")!.renderOriginal()
        navigationItem.rightBarButtonItem = UIBarButtonItem(image: img, style: .plain, target: self, action: #selector(点击查看购物车))
        
        nameLabel.text = goods.name
        serialNumLabel.text = LocalStr("商品编号:").appending(goods.id)
        unitPriceLabel.text = goods.priceStr
        totalPriceLabel.text = String(format: "%.2f", goods.price*Double(goods.num))
        amountTF.text = goods.num.description
        minusBtn.isEnabled = goods.num > 1
    }
    
    // MARK: - Event respond
    /// 点击减号
    @IBAction func tapMinusBtn() {
        amountTF.resignFirstResponder()
        var amount = Int(amountTF.text!)
        if amount != nil {
            amount = amount!-1
            minusBtn.isEnabled = amount! > 1
            amountTF.text = String(amount!)
            goods.num = amount!
            totalPriceLabel.text = String(format: "%.2f", goods.price*Double(goods.num))
        }
    }
    
    /// 点击加号
    @IBAction func tapPlusBtn() {
        amountTF.resignFirstResponder()
        var amount = Int(amountTF.text!)
        if amount != nil {
            amount = amount!+1
            minusBtn.isEnabled = amount! > 1
            amountTF.text = String(amount!)
            goods.num = amount!
            totalPriceLabel.text = String(format: "%.2f", goods.price*Double(goods.num))
        }
    }
    
    func 点击查看购物车() {
        let vc = ShoppingCartVC()
        navigationController!.pushViewController(vc, animated: true)
    }
    
    /// 数量输入框内容改变
    @IBAction func amountTFValueChange() {
        let amount = Int(amountTF.text!)
        minusBtn.isEnabled = amount != nil && amount! > 1
    }
    
    /// 数量输入框结束编辑
    @IBAction func amountTFEndEdit() {
        let amount = Int(amountTF.text!)
        if amount == nil || amount! == 0 {
            amountTF.text! = "1"
            goods.num = 1
        } else {
            goods.num = amount!
        }
        totalPriceLabel.text = String(format: "%.2f", goods.price*Double(goods.num))
    }
    
    @IBAction func tap加入购物车() {
        UserShare.shared.addToCart(goods)
        UserShare.shared.saveShoppingCart()
        
        let navView = self.navigationController!.view!
        navView.isUserInteractionEnabled = false
        let hud = MBProgressHUD.showAdded(to: navView, animated: true)
        hud.isUserInteractionEnabled = false
        hud.removeFromSuperViewOnHide = true
        hud.mode = .customView
        hud.bezelView.color = UIColor.black.withAlphaComponent(0.6)
        hud.customView = UIImageView(image: #imageLiteral(resourceName: "icon_tianjiachenggong"))
        hud.detailsLabel.text = LocalStr("添加成功，在购物车等你")
        hud.detailsLabel.textColor = UIColor.white
        hud.detailsLabel.font = UIFont.appRegularFont(ofPX: 14)
        hud.hide(animated: false, afterDelay: 1.5)
        hud.completionBlock = { () in
            navView.isUserInteractionEnabled = true
            self.navigationController!.popViewController(animated: true)
        }
    }
    
    @IBAction func tap立即购买() {
        let vc = ConfirmOrderVC()
        vc.list = [goods]
        navigationController!.pushViewController(vc, animated: true)
    }
    
    @IBAction func 点击背景() {
        amountTF.resignFirstResponder()
    }
    
    // MARK: - Get Set
    
    var goods: GoodsModel!
    
    @IBOutlet weak var nameLabel: UILabel!
    @IBOutlet weak var serialNumLabel: UILabel!
    @IBOutlet weak var unitPriceLabel: UILabel!
    @IBOutlet weak var totalPriceLabel: UILabel!
    
    @IBOutlet var minusBtn: UIButton!
    @IBOutlet var amountTF: UITextField!
    @IBOutlet var plusBtn: UIButton!
}
